package homeworks.cjt2010.homework1;

import java.util.Date;

/**
 * Describes a model of <code>a flight</code>
 * <p>
 * It contents information for airplane,source and destination airports,
 * departure and arrival time.
 * <p>
 * This class is <code>time-zone sensitive</code> and is immutable.
 * <p>
 * This class has a relation with the following classes: {@link Airplane
 * Airplane} and {@link Airport Airport enumeration class}.
 * 
 * @author Tsvetan Vasilev
 * 
 */
public final class Flight {

	// --- Instance Variables --------------------------------------------------

	/**
	 * Date/Time in the time zone of source airport
	 */
	private final Date departureTime;
	/**
	 * Date/Time in the time zone of destination airport
	 */
	private final Date arrivalTime;
	private final Airport sourceAirport;
	private final Airport destinationAirport;
	private final Airplane plane;

	// --- Constructors --------------------------------------------------------
	public Flight(Date departureTime, Date arrivalTime, Airport sourceAirport,
			Airport destinationAirport, Airplane plane) {
		this.departureTime = departureTime;
		this.arrivalTime = arrivalTime;
		this.sourceAirport = sourceAirport;
		this.destinationAirport = destinationAirport;
		this.plane = plane;
	}

	// --- Getters -------------------------------------------------------------
	public Date getDepartureTime() {
		return departureTime;
	}

	public Date getArrivalTime() {
		return arrivalTime;
	}

	public Airport getSourceAirport() {
		return sourceAirport;
	}

	public Airport getDestinationAirport() {
		return destinationAirport;
	}

	public Airplane getPlane() {
		return plane;
	}

	// --- Methods -------------------------------------------------------------

	@Override
	public int hashCode() {

		// all fields of Flight are used in equals() method. So they all have to
		// take part in calculation of the hashCode.
		return departureTime.hashCode() ^ arrivalTime.hashCode()
				^ sourceAirport.hashCode() ^ destinationAirport.hashCode()
				^ plane.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Flight)) {
			throw new IllegalArgumentException("Incorrect type of input"
					+ " parameter in equals() method of Flight class");
		}
		Flight inputFlight = (Flight) obj;
		boolean result = departureTime.equals(inputFlight.getDepartureTime())
				&& arrivalTime.equals(inputFlight.getArrivalTime())
				&& sourceAirport.equals(inputFlight.getSourceAirport())
				&& destinationAirport.equals(inputFlight
						.getDestinationAirport())
				&& plane.equals(inputFlight.getPlane());

		return result;
	}

	// work version of toString()
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(50);
		sb.append("Flight: ");
		sb.append(", SA= ").append(sourceAirport);
		sb.append(", DA= ").append(destinationAirport);
		return sb.toString();
	}

	// @Override
	// public String toString() {
	// StringBuilder sb = new StringBuilder(50);
	// sb.append("Flight: ");
	// sb.append("departureTime= ").append(departureTime);
	// sb.append(", arrivalTime= ").append(arrivalTime);
	// sb.append(", sourceAirport= ").append(sourceAirport);
	// sb.append(", destinationAirport= ").append(destinationAirport);
	// sb.append(", plane= ").append(plane);
	// return sb.toString();
	// }
}
